﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IWshRuntimeLibrary;
using System.Reflection;
using System.IO;

namespace CCF20.Shared
{
    public partial class CCF20Helper
    {
        public static void CreateShortCut(string argument,string description,string targetPath,string workingDirectory)
        {
            WshShellClass shell = new WshShellClass();
            string desktopPath = string.Format("{0}\\{1}.lnk",Environment.GetFolderPath(Environment.SpecialFolder.Desktop), description);
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(desktopPath);
            Assembly code = Assembly.GetExecutingAssembly();
            shortcut.TargetPath = targetPath;
            shortcut.Arguments = argument;
            shortcut.Description = description;
            shortcut.WorkingDirectory = workingDirectory;
            shortcut.Save();

            string company = string.Empty;
            if (Attribute.IsDefined(code, typeof(AssemblyCompanyAttribute)))
            {
                AssemblyCompanyAttribute ascompany = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code, typeof(AssemblyCompanyAttribute));
                company = ascompany.Company;
            }
            else
            {
                throw new ArgumentException("No company attribute found for the assembly");
            }
            string programMenuPath = string.Empty;
            programMenuPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", company);
            if (!Directory.Exists(programMenuPath))
            {
                Directory.CreateDirectory(programMenuPath);
            }
            programMenuPath = string.Format("{0}\\{1}.lnk", programMenuPath, description);
            System.IO.File.Copy(desktopPath, programMenuPath, true);

        }
        public static void RemoveShortCut(string description)
        {

            string desktopPath = string.Format("{0}\\{1}.lnk", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), description);
            if (System.IO.File.Exists(desktopPath))
            {
                System.IO.File.Delete(desktopPath);
            }
            Assembly code = Assembly.GetExecutingAssembly();
            string company = string.Empty;
            if (Attribute.IsDefined(code, typeof(AssemblyCompanyAttribute)))
            {
                AssemblyCompanyAttribute ascompany = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code, typeof(AssemblyCompanyAttribute));
                company = ascompany.Company;
            }
            else
            {
                throw new ArgumentException("No company attribute found for the assembly");
            }
            string programMenuPath = string.Empty;
            programMenuPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", company);
            if (Directory.Exists(programMenuPath))
            {
                programMenuPath = string.Format("{0}\\{1}.lnk", programMenuPath, description);
                if (System.IO.File.Exists(programMenuPath))
                {
                    System.IO.File.Delete(programMenuPath);
                }
            }
            
        }
    }
}
